package itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import itheima.reggie.utils.BaseContext;
import itheima.reggie.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 自定义数据对象处理器
 *
 */
@Component
@Slf4j
public class MyMetaObjectHandel implements MetaObjectHandler {
    /**
     * 插入操作，自动填充
     * @param metaObject
     */
    @Autowired
    HttpServletRequest request;
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]....");
        log.info(metaObject.toString());
        metaObject.setValue("createTime", DateUtils.DateFormat(new Date()));
        metaObject.setValue("updateTime", DateUtils.DateFormat(new Date()));
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }

    /**
     * 更新操作自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]....");
       log.info("获取request：{}",request.getSession().getAttribute("employee"));

        metaObject.setValue("updateTime", DateUtils.DateFormat(new Date()));
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}
